<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>信息流管理</el-breadcrumb-item>
      <el-breadcrumb-item>产品名称管理</el-breadcrumb-item>
    </el-breadcrumb>

    <Container class="u-mb15">
      <el-form
        inline
        class="m-form--filter"
      >
        <el-form-item>
          <el-select
            size="mini"
            class="u-w160"
            filterable
            placeholder="请选择产品名称"
            v-model="filter.productNameId"
          >
            <el-option
              v-for="item in productOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            size="mini"
            class="u-w160"
            filterable
            placeholder="请选择产品类型"
            v-model="filter.productType"
          >
            <el-option
              v-for="(item, index) in [{label: '全部产品类型'}].concat(PRODUCT_TYPE_OPTION)"
              :key="index"
              :value="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            size="mini"
            class="u-w160"
            filterable
            placeholder="请选择产品包名"
            v-model="filter.packageName"
          >
            <el-option
              v-for="(item, index) in packageOptions"
              :key="index"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            @click="search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </Container>

    <Container>
      <el-form
        inline
        class="m-table--handle"
      >
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            @click="openDialogProduceName('create')"
          >新增</el-button>
        </el-form-item>
        <CustomTableColumn :columns.sync="tableColumn" />
      </el-form>
      <el-table
        :data="tableData"
        border
        stripe
      >
        <el-table-column
          align="center"
          v-for="(item, index) in filterTableColumn"
          :key="index"
          :prop="item.prop"
          :label="item.label"
        >
          <template slot-scope="scope">
            <!-- 是否启用 -->
            <template v-if="item.prop === 'status'">
              <el-switch
                :width="34"
                :value="scope.row.status"
                :active-value="2"
                :inactive-value="1"
                @change="changeStatus(scope.row.id, scope.row.status === 1 ? 2 : 1)"
              ></el-switch>
            </template>
            <!-- 其他 -->
            <template v-else>{{scope.row[item.prop]}}</template>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="openDialogProduceName('edit', scope.row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        :page-sizes="[50, 100, 500, 1000]"
        :page-size="page.pageSize"
        layout="total, prev, pager, next, sizes"
        :total="pageTotal"
        @size-change="handlePagerSizeChange"
        @current-change="handlePagerCurrentChange"
      />
    </Container>

    <!-- 新增/编辑产品名称 -->
    <el-dialog
      width="430px"
      :title="dialogProduceNameTitle"
      :visible.sync="dialogProduceNameVisible"
    >
      <el-form
        label-width="90px"
        label-position="right"
        :model="dialogProduceData"
        :rules="dialogProduceRules"
        ref="dialogProduceForm"
      >
        <el-form-item
          label="产品名称"
          required
          prop="productName"
        >
          <el-input
            size="small"
            class="u-w300"
            placeholder="请输入产品名称"
            v-model.trim="dialogProduceData.productName"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="产品类型"
          required
          prop="productType"
        >
          <el-select
            size="small"
            class="u-w300"
            placeholder="请选择产品类型"
            filterable
            v-model="dialogProduceData.productType"
            @change="handleDialogProductType"
          >
            <el-option
              v-for="(item, index) in PRODUCT_TYPE_OPTION"
              :key="index"
              :value="item.label"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          required
          prop="packageName"
          label="产品包名"
          v-if="isVisiblePackage"
        >
          <el-input
            size="small"
            class="u-w300"
            placeholder="请输入产品包名"
            v-model.trim="dialogProduceData.packageName"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row
        type="flex"
        justify="end"
      >
        <el-button
          size="small"
          @click="dialogProduceNameVisible = false"
        >取消</el-button>
        <el-button
          type="primary"
          size="small"
          @click="handleEnsure"
        >确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import Container from "@/components/container";
import CustomTableColumn from "@/components/custom-table-column";

import { PRODUCT_TYPE_OPTION } from "@/assets/js/flow-option";
export default {
  name: "flow-product-name",
  components: {
    Container,
    CustomTableColumn
  },
  data() {
    return {
      PRODUCT_TYPE_OPTION,

      // 下拉
      productOptions: [],
      packageOptions: [],

      // 筛选
      filter: {
        productNameId: null,
        productType: "全部产品类型",
        packageName: "全部产品包名"
      },

      // 产品名称
      dialogProduceNameTitle: "",
      dialogProduceNameVisible: false,
      dialogProduceNameAction: "",
      dialogProduceData: {},
      dialogProduceRules: {
        productName: {
          required: true,
          message: "请输入产品名称",
          trigger: ["change", "blur"]
        },
        productType: {
          required: true,
          message: "请选择产品类型",
          trigger: ["change", "blur"]
        },
        packageName: {
          required: true,
          validator: (rule, value, callback) => {
            if (this.isVisiblePackage && !value) {
              callback(new Error("请输入产品包名"));
            } else {
              callback();
            }
          },
          trigger: ["change", "blur"]
        }
      },

      // 表格
      tableColumn: [
        {
          prop: "productName",
          label: "产品名称",
          visible: true
        },
        {
          prop: "productType",
          label: "产品类型",
          visible: true
        },
        {
          prop: "packageName",
          label: "产品包名",
          visible: true
        },
        {
          prop: "status",
          label: "是否上架",
          visible: true
        }
      ],

      tableData: [],
      page: {
        pageNo: 1,
        pageSize: 50
      },
      pageTotal: 0
    };
  },
  computed: {
    filterTableColumn() {
      return this.tableColumn.filter(item => item.visible);
    },
    isVisiblePackage() {
      let current = PRODUCT_TYPE_OPTION.find(
        item => item.label === this.dialogProduceData.productType
      );
      if (current) {
        return current.package;
      }
      return false;
    }
  },
  methods: {
    fetchProductOptions() {
      this.$http({
        method: "get",
        url: "/manage/api/bus/product_name/get_product_name",
        params: {
          status: 0
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.productOptions = [
            {
              id: null,
              name: "全部产品"
            }
          ].concat(res.data.data);
        }
      });
    },
    fetchPackageOptions() {
      this.$http({
        method: "get",
        url: "/manage/api/bus/product_name/get_package_name"
      }).then(res => {
        if (res.data.code === 0) {
          this.packageOptions = [
            {
              name: "全部产品包名"
            }
          ].concat(res.data.data);
        }
      });
    },
    search() {
      this.page.pageNo = 1;
      this.getTable();
    },
    getTable() {
      this.$http({
        method: "post",
        url: "/manage/api/bus/product_name/get_list",
        data: {
          ...this.filter,
          ...this.page,
          productType:
            this.filter.productType === "全部产品类型"
              ? null
              : this.filter.productType,
          packageName:
            this.filter.packageName === "全部产品包名"
              ? null
              : this.filter.packageName
        }
      }).then(res => {
        if (res.data.code === 0) {
          let data = res.data.data;
          this.tableData = data.list;
          this.pageTotal = data.listCount;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    changeStatus(id, status) {
      this.$http({
        method: "post",
        url: "/manage/api/bus/product_name/status",
        params: {
          ids: id,
          status
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.$message.success(`${status === 1 ? "停用" : "启用"}成功！`);
          this.getTable();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    openDialogProduceName(action, row) {
      if (action === "edit") {
        let { id, productName, productType, packageName } = row;
        this.dialogProduceNameTitle = "编辑";
        this.dialogProduceData = {
          id,
          productName,
          productType,
          packageName
        };
      } else {
        this.dialogProduceNameTitle = "新增";
        this.dialogProduceData = {
          productName: "",
          productType: "",
          packageName: ""
        };
      }
      this.dialogProduceNameAction = action;
      this.dialogProduceNameVisible = true;
      this.$refs.dialogProduceForm && this.$refs.dialogProduceForm.resetFields();
    },
    handleDialogProductType() {
      if (!this.isVisiblePackage) {
        this.dialogProduceData.packageName = "";
      }
    },
    // 确定
    handleEnsure() {
      if (this.dialogProduceNameAction === "edit") {
        this.updateDetail();
      } else {
        this.createDetail();
      }
    },
    // 新建
    createDetail() {
      this.$refs.dialogProduceForm.validate(valid => {
        if (valid) {
          this.$http({
            method: "post",
            url: "/manage/api/bus/product_name/add",
            data: this.dialogProduceData
          }).then(res => {
            if (res.data.code === 0) {
              this.$message.success("新建成功！");
              this.dialogProduceNameVisible = false;
              this.search();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        }
      });
    },
    // 编辑
    updateDetail() {
      this.$refs.dialogProduceForm.validate(valid => {
        if (valid) {
          this.$http({
            method: "post",
            url: "/manage/api/bus/product_name/update",
            data: this.dialogProduceData
          }).then(res => {
            if (res.data.code === 0) {
              this.$message.success("修改成功！");
              this.dialogProduceNameVisible = false;
              this.getTable();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        }
      });
    },
    // 分页
    handlePagerSizeChange(v) {
      this.page.pageSize = v;
      this.getTable();
    },
    handlePagerCurrentChange(v) {
      this.page.pageNo = v;
      this.getTable();
    }
  },
  activated(){
    this.getTable();
  },
  created() {
    this.fetchProductOptions();
    this.fetchPackageOptions();
  }
};
</script>
